/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package elaborate.h2x.transform.position;

import elaborate.h2x.transform.Relation;
import elaborate.h2x.transform.RelationType;
import elaborate.h2x.transform.TransformingModel;
import elaborate.h2x.transform.TransformingNode;
import elaborate.h2x.transform.TransformingNodeId;
import java.util.Iterator;
import org.w3c.dom.Node;

/**
 * position nodes by applying position indicated
 * using h2x:relation annotations
 * @author lendle
 */
public class IndicatedTransformingNodesPositionerImpl implements TransformingNodesPositioner{

    public void determinePositions(TransformingModel model, PositionMap map) throws Exception {
        Iterator<TransformingNode> it=model.iterateAllNodes();
        while(it.hasNext()){
            TransformingNode node=it.next();
            if(node.getRelationList()!=null && node.getRelationList().isEmpty()==false){
                Position position=new Position();
                for(Relation relation : node.getRelationList()){
                    if(relation.getRelationType().equals(RelationType.CHILD)){
                        position.setParent(relation.getContext());
                    }
                    else{
                        position.setPrev(relation.getContext());
                    }
                }
                if(position.getParent()==null){
                    position.setParent(map.getPositionSlot(node.getId()).getParent());
                }
                map.positionNode(position.getParent(), position.getPrev(), node.getId());
            }
        }
    }

    static class Position{

        protected TransformingNodeId parent;
        protected TransformingNodeId prev;

        /**
         * Get the value of prev
         *
         * @return the value of prev
         */
        public TransformingNodeId getPrev() {
            return prev;
        }

        /**
         * Set the value of prev
         *
         * @param prev new value of prev
         */
        public void setPrev(TransformingNodeId prev) {
            this.prev = prev;
        }


        /**
         * Get the value of parent
         *
         * @return the value of parent
         */
        public TransformingNodeId getParent() {
            return parent;
        }

        /**
         * Set the value of parent
         *
         * @param parent new value of parent
         */
        public void setParent(TransformingNodeId parent) {
            this.parent = parent;
        }

    }

}
